/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConductoresAleatoriosDialog.java
 *
 * Created on 02-may-2011, 23:44:41
 */

package ia_psr.transportesGUI;

import ia_psr.problemas.transportes.Conductor;
import ia_psr.problemas.transportes.EmpresaTransportes;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

/**
 *
 * @author Rubén Avalos Elvira y Fabiana Celiberti Tapie
 */
public class ConductoresAleatoriosDialog extends javax.swing.JFrame {
    private JPanel parent;

    /** Creates new form ConductoresAleatoriosDialog */
    public ConductoresAleatoriosDialog(JPanel parent) {
        this.parent = parent;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtConductoresCantidad = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        btnConductoresAceptar = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        btnCancelar = new javax.swing.JButton();

        setTitle("ETaskScheduler - Generación Conductores aleatorios");

        jLabel1.setText("Cantidad:");

        btnConductoresAceptar.setText("Aceptar");
        btnConductoresAceptar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnConductoresAceptarMouseReleased(evt);
            }
        });

        jLabel2.setText("Conductores aleatorios");

        btnCancelar.setText("Cancelar");
        btnCancelar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                btnCancelarMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnConductoresAceptar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jLabel2)
                        .addComponent(jSeparator2)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtConductoresCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtConductoresCantidad, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnConductoresAceptar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCancelarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnCancelarMouseReleased
        setVisible(false);
    }//GEN-LAST:event_btnCancelarMouseReleased

    private void btnConductoresAceptarMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_btnConductoresAceptarMouseReleased
        try {
            int cantidad = Integer.parseInt(txtConductoresCantidad.getText());
            Conductor c;
            EmpresaTransportes et = EmpresaTransportes.getInstance();
            et.removeAllConductores();

            for (int i = 1; i <= cantidad; ++i) {
                c = new Conductor("C" + i);
                et.addConductor(c);
            }
            
            setVisible(false);
            ((ConductoresPanel)parent).populateConductoresList();

        } catch (Exception e) {
            // mostrar error dialog
            JFrame frame = new JFrame();
            JOptionPane.showMessageDialog(frame,
                    "Datos no válidos. Por favor, asegúrese de introducir una cantidad numérica.",
                    "Atención",
                    JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnConductoresAceptarMouseReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnConductoresAceptar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField txtConductoresCantidad;
    // End of variables declaration//GEN-END:variables

}
